package com.eedi.framework.permission.controller.sys;

import com.eedi.framework.permission.controller.sys.vo.role.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.permission.controller.sys.vo.role.*;
import com.eedi.framework.permission.dal.dataobject.SysRoleDO;
import com.eedi.framework.permission.enums.SysRoleCodeEnum;
import com.eedi.framework.permission.service.SysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;
import static java.util.Collections.singleton;

@Tag(name = "系统界面 - 角色")
@RestController
@RequestMapping("/role")
@Validated
public class SysRoleController {

    @Resource
    private SysPermissionService sysPermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    @PreAuthorize("@sys.hasPermission('sys:role:create')")
    public CommonResult<String> createRole(@Valid @RequestBody SysRoleSaveReq createReqVO) {
        return success(sysPermissionService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("@sys.hasPermission('sys:role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody SysRoleUpReq updateReqVO) {
        sysPermissionService.updateRole(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改角色状态")
    @PreAuthorize("@sys.hasPermission('sys:role:update')")
    public CommonResult<Boolean> updateRoleStatus(@Valid @RequestBody SysRoleUpdateStatusReq reqVO) {
        sysPermissionService.updateRoleStatus(reqVO.getSysRoleId(), reqVO.getSysRoleStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Parameter(name = "sysRoleId", description = "角色编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:role:delete')")
    public CommonResult<Boolean> deleteRole(@RequestParam("sysRoleId") String sysRoleId) {
        sysPermissionService.deleteRole(sysRoleId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色信息")
    @PreAuthorize("@sys.hasPermission('sys:role:query')")
    public CommonResult<SysRoleResp> getRole(@RequestParam("sysRoleId") String sysRoleId) {
        SysRoleDO role = sysPermissionService.getRole(sysRoleId);
        return success(BeanUtils.toBean(role, SysRoleResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色分页")
    @PreAuthorize("@sys.hasPermission('sys:role:query')")
    public CommonResult<PageResult<SysRoleResp>> getRolePage(SysRolePageReq pageReqVO) {
        pageReqVO.setSysRoleCode(SysRoleCodeEnum.DEFAULT.getCode());
        PageResult<SysRoleDO> pageResult = sysPermissionService.getRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysRoleResp.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色，主要用于前端的下拉选项")
    public CommonResult<List<SysRoleSimpleResp>> getSimpleRoleList() {
        List<SysRoleDO> list = sysPermissionService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE));
        list.sort(Comparator.comparing(SysRoleDO::getSysRoleSort));
        List<SysRoleDO> filterList = list.stream().filter(x -> !SysRoleCodeEnum.SYS_ADMIN.getCode().equals(x.getSysRoleCode())).toList();
        return success(BeanUtils.toBean(filterList, SysRoleSimpleResp.class));
    }




    @GetMapping("/get-roles")
    @Operation(summary = "根据系统用户ID获取角色列表")
    @Parameter(name = "sysUserId", description = "用户编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:role:get-roles')")
    public CommonResult<List<SysRoleSimpleResp> > getRoles(String sysUserId) {
        return success(sysPermissionService.getBySysUserId(sysUserId));
    }

}
